import tkinter
import tkinter as tk
from tkinter import GROOVE, ttk, messagebox, DISABLED
from tkinter.messagebox import askyesno, showinfo
from tkinter.ttk import Scrollbar

import pymysql

import Login_Window
from ttkbootstrap import Style


class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("主窗体")
        self.geometry("1000x640+342+123")
        self.resizable(0, 0)
        # self["bg"]="royalblue"
        # 加载gui
        style = Style(theme='united')
        style.configure('TLabel')
        style.configure('TButton')
        style.configure('TRadiobutton')
        style.configure('TPanedwindow')
        style.configure('TLabelframe')
        style.configure('Treeview')
        self.setup_UI()

    def setup_UI(self):
        mebubar = tk.Menu(self, relief=GROOVE)
        mebubar.add_command(label="学生信息", command=self.student_info)
        mebubar.add_command(label="学院信息", command=self.college_window)
        mebubar.add_command(label="教师信息", command=self.teacher_info)

        mebubar.add_command(label="切换用户", command=self.cancellation)
        self.config(menu=mebubar)

    def student_info(self):
        self.Pane_center = ttk.PanedWindow(width=1000, height=540, style="TPanedwindow")

        # LabelFrame
        LabelFrame_query = ttk.LabelFrame(self.Pane_center, text="学生信息查询", width=690, height=70, style='TLabelframe')
        LabelFrame_query.place(x=10, y=10)
        # 添加控件
        Label_sno = ttk.Label(LabelFrame_query, text="学号：", style='TLabel')
        Label_sno.place(x=5, y=13)
        select_sno = tk.StringVar()
        self.Entry_sno = ttk.Entry(LabelFrame_query, width=10, textvariable=select_sno, style='TEntry')
        self.Entry_sno.place(x=65, y=10)
        Button_query = ttk.Button(LabelFrame_query, text="添加", width=4, command=self.insert, style='TButton')
        Button_query.place(x=280, y=10)
        Button_query = ttk.Button(LabelFrame_query, text="查询", width=4, command=self.Select_sno, style='TButton')
        Button_query.place(x=350, y=10)
        Button_query = ttk.Button(LabelFrame_query, text="修改", width=4, command=self.update_student_window,
                                  style='TButton')
        Button_query.place(x=420, y=10)
        Button_query = ttk.Button(LabelFrame_query, text="删除", width=4, command=self.delete_student, style='TButton')
        Button_query.place(x=490, y=10)
        Button_all = ttk.Button(LabelFrame_query, text="刷新", width=8, command=self.student_info, style='TButton')
        Button_all.place(x=560, y=10)

        self.Tree = tkinter.ttk.Treeview(self.Pane_center, columns=("sno", "names",
                                                                    "gender", "birthday", "college", "mobile",
                                                                    "address",
                                                                    "email"), style='Treeview',
                                         show="headings",
                                         height=20)
        # 设置每一个列的宽度和对齐的方式
        self.Tree.column("sno", width=100, anchor="center")
        self.Tree.column("names", width=70, anchor="center")
        self.Tree.column("gender", width=70, anchor="center")
        self.Tree.column("birthday", width=70, anchor="center")
        self.Tree.column("college", width=100, anchor="center")
        self.Tree.column("mobile", width=100, anchor="center")
        self.Tree.column("address", width=120, anchor="center")
        self.Tree.column("email", width=100, anchor="center")

        # 设置每个列的标题
        self.Tree.heading("sno", text="学号")
        self.Tree.heading("names", text="姓名")
        self.Tree.heading("gender", text="性别")
        self.Tree.heading("birthday", text="年龄")
        self.Tree.heading("college", text="学院")
        self.Tree.heading("mobile", text="手机号码")
        self.Tree.heading("address", text="家庭住址")
        self.Tree.heading("email", text="邮箱地址")
        self.Tree.bind('<ButtonRelease-1>', self.treeviewClick)
        self.Tree.place(x=10, y=80)
        self.Pane_center.place(x=120, y=54)

        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sql = 'select * from student order by 学号 '
        cursor.execute(sql)
        result = cursor.fetchall()
        for index in range(len(result)):
            self.Tree.insert("", index, values=(result[index][0], result[index][1],
                                                result[index][2], result[index][3], result[index][4],
                                                result[index][5], result[index][6], result[index][7]))
        cursor.close()
        conn.close()

    def insert(self):
        # 学生信息明细窗口
        Detail_Window = tk.Toplevel(self)
        Detail_Window.geometry('600x500+500+100')
        Detail_Window.title('学生信息')
        # 学号
        new_sno = tk.StringVar()  # 将输入的注册名赋值给变量
        ttk.Label(Detail_Window, text='学号: ', style='TLabel').place(x=30, y=10)  # 将`User name:`放置在坐标（10,10）。
        self.entry_new_sno = ttk.Entry(Detail_Window, textvariable=new_sno,
                                       style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.entry_new_sno.place(x=100, y=10, width=120)
        # 姓名
        new_name = tk.StringVar()  # 将输入的注册名赋值给变量
        ttk.Label(Detail_Window, text='姓名: ', style='TLabel').place(x=250, y=10)  # 将`User name:`放置在坐标（10,10）。
        self.entry_new_name = ttk.Entry(Detail_Window, textvariable=new_name,
                                        style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.entry_new_name.place(x=300, y=10, width=60)
        # 性别
        self.new_sex = tk.StringVar()
        ttk.Label(Detail_Window, text='性别：', style='TLabel').place(x=30, y=60)
        Radio_man = ttk.Radiobutton(Detail_Window, text="男", variable=self.new_sex, value='男', style='TRadiobutton')
        Radio_man.place(x=100, y=60)
        Radio_woman = ttk.Radiobutton(Detail_Window, text="女", variable=self.new_sex, value='女', style='TRadiobutton')
        Radio_woman.place(x=150, y=60)
        # 年龄
        new_age = tk.StringVar()
        ttk.Label(Detail_Window, text='年龄：', style='TLabel').place(x=250, y=60)
        self.entry_new_age = ttk.Entry(Detail_Window, textvariable=new_age,
                                       style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.entry_new_age.place(x=300, y=60, width=60)
        # 学院
        new_college = tk.StringVar()
        ttk.Label(Detail_Window, text='学院：', style='TLabel').place(x=30, y=110)
        self.entry_new_college = ttk.Entry(Detail_Window, textvariable=new_college,
                                           style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.entry_new_college.place(x=100, y=110, width=120)
        # 手机号码
        new_mobile = tk.StringVar()
        ttk.Label(Detail_Window, text='手机号码：', style='TLabel').place(x=250, y=110)
        self.entry_new_mobile = ttk.Entry(Detail_Window, textvariable=new_mobile,
                                          style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.entry_new_mobile.place(x=330, y=110, width=120)
        # 联系地址
        new_address = tk.StringVar()
        ttk.Label(Detail_Window, text='联系地址：', style='TLabel').place(x=30, y=160)
        self.entry_new_address = ttk.Entry(Detail_Window, textvariable=new_address,
                                           style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.entry_new_address.place(x=100, y=160, width=120)
        # 邮箱地址t
        new_email = tk.StringVar()
        ttk.Label(Detail_Window, text='邮箱地址：', style='TLabel').place(x=250, y=160)
        self.entry_new_email = ttk.Entry(Detail_Window, textvariable=new_email,
                                         style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.entry_new_email.place(x=330, y=160, width=120)
        # 确定按钮
        btn = ttk.Button(Detail_Window, text='添加', width=10, command=self.insert_student_mysql, style='TButton')
        btn.place(x=250, y=210)

    def insert_student_mysql(self):
        a = self.entry_new_sno.get()
        b = self.entry_new_name.get()
        c = self.new_sex.get()
        d = self.entry_new_age.get()
        e = self.entry_new_college.get()
        f = self.entry_new_mobile.get()
        g = self.entry_new_address.get()
        h = self.entry_new_email.get()

        # 数据库连接
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sql = 'select * from student'
        cursor.execute(sql)
        result = cursor.fetchall()
        # 将数据用字典形式存储于result
        result = [dict(zip([k[0] for k in cursor.description], row)) for row in result]
        ulist = []
        for item in result:
            ulist.append(item['学号'])
        try:
            if a in ulist:
                conn.rollback()
                messagebox.showerror('警告', message='学号已存在')
            elif a == '' or b == '' or c == '' or d == '' or e == '' or f == '' or g == '' or h == '':
                conn.rollback()
                messagebox.showerror('警告', message='信息不能为空')
            else:
                # 提交事务
                cursor.execute('INSERT INTO student(学号,名字,性别,年龄,学院,手机号码,联系地址,邮箱地址) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)',
                               (a, b, c, d, e, f, g, h))
                conn.commit()
                cursor.execute('INSERT INTO 登陆账户(user,password) VALUES(%s,%s)', (a, '123456'))
                conn.commit()
                messagebox.showinfo(title='恭喜', message='添加学生信息成功')
            cursor.close()
            conn.close()
        except:
            # 限制提交
            conn.rollback()

    def Select_sno(self):
        for i in self.Tree.get_children():
            self.Tree.delete(i)
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sno = self.Entry_sno.get()

        sql = "select * from student where 学号 = %s" % (sno)
        cursor.execute(sql)
        result = cursor.fetchall()
        print(result)
        for index in range(len(result)):
            self.Tree.insert("", index, values=(result[index][0], result[index][1],
                                                result[index][2], result[index][3], result[index][4],
                                                result[index][5], result[index][6], result[index][7]))
        cursor.close()
        conn.close()

    def treeviewClick(self, event):
        for item in self.Tree.selection():
            self.item_text = self.Tree.item(item, "values")

    def teacher_treeviewClick(self, event):
        for item in self.Teacher_Tree.selection():
            self.teacher_item_text = self.Teacher_Tree.item(item, "values")

    def update_student_window(self):
        info = self.item_text
        print(info)
        Detail_Window = tk.Toplevel(self)
        Detail_Window.geometry('600x500+500+100')
        Detail_Window.title('学生信息')
        # 学号

        self.new_sno = tk.StringVar()  # 将输入的注册名赋值给变量
        ttk.Label(Detail_Window, text='学号: ', style='TLabel').place(x=30, y=10)  # 将`User name:`放置在坐标（10,10）。
        self.Update_sno = ttk.Entry(Detail_Window, textvariable=self.new_sno,
                                    style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.new_sno.set(info[0])
        self.Update_sno["state"] = DISABLED
        self.Update_sno.place(x=100, y=10, width=120)
        # 姓名
        self.Update_name = tk.StringVar()  # 将输入的注册名赋值给变量
        ttk.Label(Detail_Window, text='姓名: ', style='TLabel').place(x=250, y=10)  # 将`User name:`放置在坐标（10,10）。
        self.entry_update_name = ttk.Entry(Detail_Window, textvariable=self.Update_name,
                                           style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.Update_name.set(info[1])
        self.entry_update_name.place(x=300, y=10, width=60)
        # 性别
        self.Update_sex = tk.StringVar()
        ttk.Label(Detail_Window, text='性别：', style='TLabel').place(x=30, y=60)
        Radio_man = ttk.Radiobutton(Detail_Window, text="男", variable=self.Update_sex, value='男', style='TRadiobutton')
        Radio_man.place(x=100, y=60)
        Radio_woman = ttk.Radiobutton(Detail_Window, text="女", variable=self.Update_sex, value='女',
                                      style='TRadiobutton')
        self.Update_sex.set(info[2])
        Radio_woman.place(x=150, y=60)
        # 年龄
        self.Update_age = tk.StringVar()
        ttk.Label(Detail_Window, text='年龄：', style='TLabel').place(x=250, y=60)
        self.entry_update_age = ttk.Entry(Detail_Window, textvariable=self.Update_age,
                                          style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.Update_age.set(info[3])
        self.entry_update_age.place(x=300, y=60, width=60)
        # 学院
        self.Update_college = tk.StringVar()
        ttk.Label(Detail_Window, text='学院：', style='TLabel').place(x=30, y=110)
        self.entry_update_college = ttk.Entry(Detail_Window,
                                              textvariable=self.Update_college,
                                              style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.Update_college.set(info[4])
        self.entry_update_college.place(x=100, y=110, width=120)
        # 手机号码
        self.Update_mobile = tk.StringVar()
        ttk.Label(Detail_Window, text='手机号码：', style='TLabel').place(x=250, y=110)
        self.entry_update_mobile = ttk.Entry(Detail_Window, textvariable=self.Update_mobile,
                                             style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.Update_mobile.set(info[5])
        self.entry_update_mobile.place(x=330, y=110, width=120)
        # 联系地址
        self.Update_address = tk.StringVar()
        ttk.Label(Detail_Window, text='联系地址：', style='TLabel').place(x=30, y=160)
        self.entry_update_address = ttk.Entry(Detail_Window,
                                              textvariable=self.Update_address,
                                              style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.Update_address.set(info[6])
        self.entry_update_address.place(x=100, y=160, width=120)
        # 邮箱地址
        self.Update_email = tk.StringVar()
        ttk.Label(Detail_Window, text='邮箱地址：', style='TLabel').place(x=250, y=160)
        self.entry_update_email = ttk.Entry(Detail_Window, textvariable=self.Update_email,
                                            style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.Update_email.set(info[7])
        self.entry_update_email.place(x=330, y=160, width=120)
        # 确定按钮
        btn = ttk.Button(Detail_Window, text='保存', width=10, command=self.update_mysql, style='TButton')
        btn.place(x=250, y=210)

    def update_mysql(self):
        s = self.Update_sno.get()

        a = self.entry_update_name.get()
        b = self.Update_sex.get()
        c = self.entry_update_age.get()
        d = self.entry_update_college.get()
        e = self.entry_update_mobile.get()
        f = self.entry_update_address.get()
        g = self.entry_update_email.get()
        # 数据库连接
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        cursor.execute(
            "update student set 名字='%s',性别='%s',年龄='%s',学院='%s',手机号码='%s',联系地址='%s',邮箱地址='%s' where 学号='%s'" % (
                a, b, c, d, e, f, g, s))
        conn.commit()
        cursor.close()
        conn.close()
        messagebox.showinfo(title='恭喜', message='修改学生信息成功')

    def delete_student(self):
        s = self.item_text[0]
        choose = askyesno("删除确认", "确定要删除该学生（学号:" + self.item_text[0] + ",姓名:" +
                          self.item_text[1] + "）的信息吗？")
        if choose:
            # 执行删除动作
            # 数据库连接
            conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
            cursor = conn.cursor()
            cursor.execute(
                "delete from student where 学号='%s'" % s)
            conn.commit()
            cursor.close()
            conn.close()
            showinfo("系统消息", "删除成功！")
        else:
            return

    def teacher_info(self):
        self.Tea_Pane_center = ttk.PanedWindow(width=1000, height=540, style="TPanedwindow")
        # LabelFrame
        LabelFrame_query = ttk.LabelFrame(self.Tea_Pane_center, text="教师信息查询", width=690, height=70,
                                          style='TLabelframe')
        LabelFrame_query.place(x=10, y=10)
        # 添加控件
        Label_sno = ttk.Label(LabelFrame_query, text="教师工号：", style='TLabel')
        Label_sno.place(x=5, y=13)
        select_sno = tk.StringVar()
        self.Teacher_Entry_sno = ttk.Entry(LabelFrame_query, width=10, textvariable=select_sno, style='TEntry')
        self.Teacher_Entry_sno.place(x=80, y=10)

        Button_query = ttk.Button(LabelFrame_query, text="添加", width=4, command=self.teacher_insert, style='TButton')
        Button_query.place(x=280, y=10)
        Button_query = ttk.Button(LabelFrame_query, text="查询", width=4, command=self.select_tacher, style='TButton')
        Button_query.place(x=350, y=10)
        Button_query = ttk.Button(LabelFrame_query, text="修改", width=4, command=self.teacher_update_window,
                                  style='TButton')
        Button_query.place(x=420, y=10)
        Button_query = ttk.Button(LabelFrame_query, text="删除", width=4, command=self.delete_teacher, style='TButton')
        Button_query.place(x=490, y=10)
        Button_all = ttk.Button(LabelFrame_query, text="刷新", width=8, command=self.teacher_info, style='TButton')
        Button_all.place(x=560, y=10)

        self.Teacher_Tree = tkinter.ttk.Treeview(self.Tea_Pane_center, columns=("sno", "names",
                                                                                "gender", "wages", "post", "birthday",
                                                                                "college",
                                                                                "mobile", "address",
                                                                                ),
                                                 show="headings", style='Treeview',
                                                 height=20)
        # 设置每一个列的宽度和对齐的方式
        self.Teacher_Tree.column("sno", width=100, anchor="center")
        self.Teacher_Tree.column("names", width=70, anchor="center")
        self.Teacher_Tree.column("gender", width=70, anchor="center")
        self.Teacher_Tree.column("wages", width=100, anchor="center")
        self.Teacher_Tree.column("post", width=100, anchor="center")
        self.Teacher_Tree.column("birthday", width=70, anchor="center")
        self.Teacher_Tree.column("college", width=100, anchor="center")
        self.Teacher_Tree.column("mobile", width=100, anchor="center")
        self.Teacher_Tree.column("address", width=120, anchor="center")

        # 设置每个列的标题
        self.Teacher_Tree.heading("sno", text="工号")
        self.Teacher_Tree.heading("names", text="姓名")
        self.Teacher_Tree.heading("gender", text="性别")
        self.Teacher_Tree.heading("wages", text="基本工资")
        self.Teacher_Tree.heading("post", text="职称")
        self.Teacher_Tree.heading("birthday", text="年龄")
        self.Teacher_Tree.heading("college", text="学院")
        self.Teacher_Tree.heading("mobile", text="手机号码")
        self.Teacher_Tree.heading("address", text="联系地址")
        self.Teacher_Tree.bind('<ButtonRelease-1>', self.teacher_treeviewClick)
        self.Teacher_Tree.place(x=10, y=80)
        self.Tea_Pane_center.place(x=120, y=54)

        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sql = 'select * from teacher '
        cursor.execute(sql)
        result = cursor.fetchall()

        for index in range(len(result)):
            self.Teacher_Tree.insert("", index, values=(result[index][0], result[index][1],
                                                        result[index][2], result[index][3], result[index][4],
                                                        result[index][5], result[index][6], result[index][7],
                                                        result[index][8]))
        cursor.close()
        conn.close()

    def teacher_update_window(self):
        teacher_info = self.teacher_item_text
        Detail_Window = tk.Toplevel(self)
        Detail_Window.geometry('600x500+500+100')
        Detail_Window.title('教师信息')
        # 教师工号

        self.t_new_sno = tk.StringVar()  # 将输入的注册名赋值给变量
        ttk.Label(Detail_Window, text='学号: ', style='TLabel').place(x=30, y=10)  # 将`User name:`放置在坐标（10,10）。
        self.t_Update_sno = ttk.Entry(Detail_Window, textvariable=self.t_new_sno,
                                      style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_new_sno.set(teacher_info[0])
        self.t_Update_sno["state"] = DISABLED
        self.t_Update_sno.place(x=100, y=10, width=120)
        # 姓名
        self.t_Update_name = tk.StringVar()  # 将输入的注册名赋值给变量
        ttk.Label(Detail_Window, text='姓名: ', style='TLabel').place(x=250, y=10)  # 将`User name:`放置在坐标（10,10）。
        self.t_entry_update_name = ttk.Entry(Detail_Window, textvariable=self.t_Update_name,
                                             style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_Update_name.set(teacher_info[1])
        self.t_entry_update_name.place(x=300, y=10, width=60)
        # 性别
        self.t_Update_sex = tk.StringVar()
        ttk.Label(Detail_Window, text='性别：', style='TLabel').place(x=30, y=60)
        Radio_man = ttk.Radiobutton(Detail_Window, text="男", variable=self.t_Update_sex, value='男',
                                    style='TRadiobutton')
        Radio_man.place(x=100, y=60)
        Radio_woman = ttk.Radiobutton(Detail_Window, text="女", variable=self.t_Update_sex, value='女',
                                      style='TRadiobutton')
        self.t_Update_sex.set(teacher_info[2])
        Radio_woman.place(x=150, y=60)
        # 基本工资
        self.t_Update_age = tk.StringVar()
        ttk.Label(Detail_Window, text='基本工资：', style='TLabel').place(x=250, y=60)
        self.t_entry_update_age = ttk.Entry(Detail_Window, textvariable=self.t_Update_age,
                                            style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_Update_age.set(teacher_info[3])
        self.t_entry_update_age.place(x=330, y=60, width=60)
        # 职称
        self.t_Update_college = tk.StringVar()
        ttk.Label(Detail_Window, text='职称：', style='TLabel').place(x=30, y=110)
        self.t_entry_update_college = ttk.Entry(Detail_Window,
                                                textvariable= self.t_Update_college,
                                                style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_Update_college.set(teacher_info[4])
        self.t_entry_update_college.place(x=100, y=110, width=120)
        # 年龄
        self.t_Update_mobile = tk.StringVar()
        ttk.Label(Detail_Window, text='年龄：', style='TLabel').place(x=250, y=110)
        self.t_entry_update_mobile = ttk.Entry(Detail_Window, textvariable= self.t_Update_mobile,
                                               style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_Update_mobile.set(teacher_info[5])
        self.t_entry_update_mobile.place(x=330, y=110, width=120)
        # 学院
        self.t_Update_address = tk.StringVar()
        ttk.Label(Detail_Window, text='学院：', style='TLabel').place(x=30, y=160)
        self.t_entry_update_address = ttk.Entry(Detail_Window,
                                                textvariable= self.t_Update_address,
                                                style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_Update_address.set(teacher_info[6])
        self.t_entry_update_address.place(x=100, y=160, width=120)
        # 手机号码
        self.t_Update_email = tk.StringVar()
        ttk.Label(Detail_Window, text='手机号码：', style='TLabel').place(x=250, y=160)
        self.t_entry_update_email = ttk.Entry(Detail_Window, textvariable=self.t_Update_email,
                                              style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_Update_email.set(teacher_info[7])
        self.t_entry_update_email.place(x=330, y=160, width=120)
        # 联系地址
        self.t_Update_e = tk.StringVar()
        ttk.Label(Detail_Window, text='联系地址：', style='TLabel').place(x=30, y=210)
        self.t_entry_update_e = ttk.Entry(Detail_Window, style='TEntry',
                                          textvariable=self.t_Update_e)  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_Update_e.set(teacher_info[8])
        self.t_entry_update_e.place(x=100, y=210, width=120)
        # 确定按钮
        btn = ttk.Button(Detail_Window, text='保存', width=10, command=self.t_update_mysql, style='TButton')
        btn.place(x=250, y=260)

    def t_update_mysql(self):
        s = self.t_Update_sno.get()

        a = self.t_entry_update_name.get()
        b = self.t_Update_sex.get()
        c = self.t_entry_update_age.get()
        d = self.t_entry_update_college.get()
        e = self.t_entry_update_mobile.get()
        f = self.t_entry_update_address.get()
        g = self.t_entry_update_email.get()
        h = self.t_entry_update_e.get()
        print(s,a, b, c, d, e, f, g, h)
        # 数据库连接
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        cursor.execute(
            "update teacher set 姓名='%s',性别='%s',基本工资='%s',职称='%s',年龄='%s',学院='%s',手机号码='%s',联系地址='%s' where 工号='%s'" % (
                a, b, c, d, e, f, g, h, s))
        conn.commit()
        cursor.close()
        conn.close()
        messagebox.showinfo(title='恭喜', message='修改教师信息成功')

    def teacher_insert(self):
        # 学生信息明细窗口
        Detail_Window = tk.Toplevel(self)
        Detail_Window.geometry('600x500+500+100')
        Detail_Window.title('教师信息')
        # 工号
        new_sno = tk.StringVar()  # 将输入的注册名赋值给变量
        ttk.Label(Detail_Window, text='工号: ', style='TLabel').place(x=30, y=10)  # 将`User name:`放置在坐标（10,10）。
        self.t_entry_new_sno = ttk.Entry(Detail_Window, textvariable=new_sno,
                                         style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_entry_new_sno.place(x=100, y=10, width=120)
        # 姓名
        new_name = tk.StringVar()  # 将输入的注册名赋值给变量
        ttk.Label(Detail_Window, text='姓名: ', style='TLabel').place(x=250, y=10)  # 将`User name:`放置在坐标（10,10）。
        self.t_entry_new_name = ttk.Entry(Detail_Window, textvariable=new_name,
                                          style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_entry_new_name.place(x=300, y=10, width=60)
        # 性别
        self.t_new_sex = tk.StringVar()
        ttk.Label(Detail_Window, text='性别：', style='TLabel').place(x=30, y=60)
        Radio_man = ttk.Radiobutton(Detail_Window, text="男", variable=self.t_new_sex, value='男', style='TRadiobutton')
        Radio_man.place(x=100, y=60)
        Radio_woman = ttk.Radiobutton(Detail_Window, text="女", variable=self.t_new_sex, value='女', style='TRadiobutton')
        Radio_woman.place(x=150, y=60)
        # 基本工资
        new_age = tk.StringVar()
        ttk.Label(Detail_Window, text='基本工资：', style='TLabel').place(x=250, y=60)
        self.t_entry_new_age = ttk.Entry(Detail_Window, textvariable=new_age,
                                         style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_entry_new_age.place(x=300, y=60, width=60)
        # 职称
        new_college = tk.StringVar()
        ttk.Label(Detail_Window, text='职称：', style='TLabel').place(x=30, y=110)
        self.t_entry_new_college = ttk.Entry(Detail_Window, textvariable=new_college,
                                             style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_entry_new_college.place(x=100, y=110, width=120)
        # 年龄
        new_mobile = tk.StringVar()
        ttk.Label(Detail_Window, text='年龄：', style='TLabel').place(x=250, y=110)
        self.t_entry_new_mobile = ttk.Entry(Detail_Window, textvariable=new_mobile,
                                            style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_entry_new_mobile.place(x=330, y=110, width=120)
        # 学院
        new_address = tk.StringVar()
        ttk.Label(Detail_Window, text='学院：', style='TLabel').place(x=30, y=160)
        self.t_entry_new_address = ttk.Entry(Detail_Window, textvariable=new_address,
                                             style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_entry_new_address.place(x=100, y=160, width=120)
        # 手机号码
        new_email = tk.StringVar()
        ttk.Label(Detail_Window, text='手机号码：', style='TLabel').place(x=250, y=160)
        self.t_entry_new_email = ttk.Entry(Detail_Window, textvariable=new_email,
                                           style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_entry_new_email.place(x=330, y=160, width=120)
        # 联系地址
        new_email = tk.StringVar()
        ttk.Label(Detail_Window, text='联系地址：', style='TLabel').place(x=30, y=210)
        self.t_entry_new_e = ttk.Entry(Detail_Window, textvariable=new_email,
                                       style='TEntry')  # 创建一个注册名的`entry`，变量为`new_name`
        self.t_entry_new_e.place(x=100, y=210, width=120)
        # 确定按钮
        btn = ttk.Button(Detail_Window, text='添加', width=10, command=self.t_insert_student_mysql, style='TButton')
        btn.place(x=250, y=210)

    def t_insert_student_mysql(self):
        a = self.t_entry_new_sno.get()
        b = self.t_entry_new_name.get()
        c = self.t_new_sex.get()
        d = self.t_entry_new_age.get()
        e = self.t_entry_new_college.get()
        f = self.t_entry_new_mobile.get()
        g = self.t_entry_new_address.get()
        h = self.t_entry_new_email.get()
        i = self.t_entry_new_e.get()

        # 数据库连接
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sql = 'select * from teacher'
        cursor.execute(sql)
        result = cursor.fetchall()
        # 将数据用字典形式存储于result
        result = [dict(zip([k[0] for k in cursor.description], row)) for row in result]
        ulist = []
        for item in result:
            ulist.append(item['工号'])
        try:
            if a in ulist:
                conn.rollback()
                messagebox.showerror('警告', message='已存在该教师的信息')
            elif a == '' or b == '' or c == '' or d == '' or e == '' or f == '' or g == '' or h == '':
                conn.rollback()
                messagebox.showerror('警告', message='信息不能为空')
            else:
                # 提交事务
                cursor.execute(
                    'INSERT INTO teacher(工号,姓名,性别,基本工资,职称,年龄,学院,手机号码,联系地址) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)',
                    (a, b, c, d, e, f, g, h, i))
                conn.commit()
                cursor.execute('INSERT INTO 登陆账户(user,password) VALUES(%s,%s)', (a, '123456'))
                conn.commit()
                messagebox.showinfo(title='恭喜', message='添加教师信息成功')
            cursor.close()
            conn.close()
        except:
            # 限制提交
            conn.rollback()

    def delete_teacher(self):
        s = self.teacher_item_text[0]
        choose = askyesno("删除确认", "确定要删除该教师的信息吗（工号:" + self.teacher_item_text[0] + ",姓名:" +
                          self.teacher_item_text[1] + "）")
        if choose:
            # 执行删除动作
            # 数据库连接
            conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
            cursor = conn.cursor()
            cursor.execute(
                "delete from teacher where 工号='%s'" % s)
            conn.commit()
            cursor.close()
            conn.close()
            showinfo("系统消息", "删除成功！")
        else:
            return

    def select_tacher(self):
        for i in self.Teacher_Tree.get_children():
            self.Teacher_Tree.delete(i)
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sno = self.Teacher_Entry_sno.get()

        sql = "select * from teacher where 工号 = %s" % (sno)
        cursor.execute(sql)
        result = cursor.fetchall()
        for index in range(len(result)):
            self.Teacher_Tree.insert("", index, values=(result[index][0], result[index][1],
                                                        result[index][2], result[index][3], result[index][4],
                                                        result[index][5], result[index][6], result[index][7]))
        cursor.close()
        conn.close()

    def cancellation(self):
        self.destroy()
        Login_Window.login_gui()

    def college_window(self):
        self.college_Pane_center = ttk.PanedWindow(width=1000, height=540, style="TPanedwindow")
        # LabelFrame_query = ttk.LabelFrame(self.college_Pane_center, text="学院信息查询", width=690, height=70,
        #                                   style='TLabelframe')
        # LabelFrame_query.place(x=10, y=10)
        # # 添加控件
        # Label_sno = ttk.Label(LabelFrame_query, text="学院名：", style='TLabel')
        # Label_sno.place(x=5, y=13)
        # select_name = tk.StringVar()
        # self.college_Entry_sno = ttk.Entry(LabelFrame_query, width=10, textvariable=select_name, style='TEntry')
        # self.college_Entry_sno.place(x=80, y=10)
        #
        # Button_query = ttk.Button(LabelFrame_query, text="查询", width=4, style='TButton')
        # Button_query.place(x=300, y=10)
        # Button_all = ttk.Button(LabelFrame_query, text="刷新", width=8, style='TButton')
        # Button_all.place(x=400, y=10)

        self.college_Tree = tkinter.ttk.Treeview(self.college_Pane_center, columns=("sno", "names", "leader"),
                                                 show="headings", style='Treeview',
                                                 height=20)
        # 设置每一个列的宽度和对齐的方式
        self.college_Tree.column("sno", width=220, anchor="center")
        self.college_Tree.column("names", width=220, anchor="center")
        self.college_Tree.column("leader", width=220, anchor="center")

        # 设置每个列的标题
        self.college_Tree.heading("sno", text="编号")
        self.college_Tree.heading("names", text="学院名")
        self.college_Tree.heading("leader", text="负责人")

        self.college_Tree.bind('<ButtonRelease-1>', self.college_treeviewClick)
        self.college_Tree.place(x=10, y=80)
        self.college_Pane_center.place(x=120, y=54)

        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sql = 'select * from college '
        cursor.execute(sql)
        result = cursor.fetchall()

        for index in range(len(result)):
            self.college_Tree.insert("", index, values=(result[index][0], result[index][1],
                                                        result[index][2]))
        cursor.close()
        conn.close()

    def college_treeviewClick(self, event):
        for item in self.college_Tree.selection():
            self.college_item_text = self.college_Tree.item(item, "values")


